package com.wfl.excel.demo.util;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.SyncReadListener;
import com.wfl.excel.demo.entity.Excel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

public class ExcelUtil {

    /**
     * 导入excel
     * @param path excel路径/名字
     */
    public static List<Excel> importExcel(String path){
        List<Excel> list = null;
        try {
            list = EasyExcel.read(
                    new FileInputStream(path),
                    Excel.class,
                    new SyncReadListener()).sheet(0).doReadSync();
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 导出excel
     * @param path excel路径/名字
     */
    public static void exportExcel(String path,List<Excel> list){
        try {
            EasyExcel.write(
                    new FileOutputStream(path), Excel.class).sheet(0).doWrite(list);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}
